﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Globalization.DateTimeFormatting;
using Windows.UI.Xaml.Data;

namespace Enough.Converter
{
    /// <summary>
    /// Value converter for concerting strings to UPPERCASE
    /// </summary>
    public class TimeSpanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            TimeSpan? span = null;
            if (value is TimeSpan)
            {
                span = (TimeSpan)value;
            }
            else if (value is DateTime)
            {
                span = DateTime.Now - (DateTime)value;
            }
            else if (value is DateTimeOffset)
            {
                span = DateTimeOffset.Now - (DateTimeOffset)value;
            }
            if (span == null)
            {
                return value;
            }
            TimeSpan timeSpan = span.Value;
            var resourceLoader = new Windows.ApplicationModel.Resources.ResourceLoader();
            string format = null;
            if (timeSpan.Days > 365)
            {
                int years = timeSpan.Days / 365;
                int months = (timeSpan.Days % 365) / 30;
                if (years > 1)
                {
                    format = months == 1 ? resourceLoader.GetString("TimeSpan_Format_YearsAndMonth") : resourceLoader.GetString("TimeSpan_Format_YearsAndMonths");
                }
                else
                {
                    format = months == 1 ? resourceLoader.GetString("TimeSpan_Format_YearAndMonth") : resourceLoader.GetString("TimeSpan_Format_YearAndMonths");
                }
                if (months == 0 || string.IsNullOrEmpty(format))
                {
                    if (years == 1)
                    {
                        format = resourceLoader.GetString("TimeSpan_Format_Year");
                        if (string.IsNullOrEmpty(format))
                        {
                            format = "1 year";
                        }
                    }
                    else
                    {
                        format = resourceLoader.GetString("TimeSpan_Format_Years");
                        if (string.IsNullOrEmpty(format))
                        {
                            format = "{0} years";
                        }
                    }
                    return string.Format(format, years);
                }
                else
                {
                    return string.Format(format, years, months);
                }
            }

            if (timeSpan.Days >= 30)
            {
                int months = timeSpan.Days / 30;
                int monthDays = timeSpan.Days % 30;
                if (monthDays == 0)
                {
                    format = months == 1 ? resourceLoader.GetString("TimeSpan_Format_Month") : resourceLoader.GetString("TimeSpan_Format_Months");
                }
                else if (monthDays == 1)
                {
                    format = months == 1 ? resourceLoader.GetString("TimeSpan_Format_MonthAndDay") : resourceLoader.GetString("TimeSpan_Format_MonthsAndDay");
                }
                else // if (monthDays > 1)
                {
                    format = months == 1 ? resourceLoader.GetString("TimeSpan_Format_MonthAndDays") : resourceLoader.GetString("TimeSpan_Format_MonthsAndDays");
                }
                if (!string.IsNullOrEmpty(format))
                {
                    return string.Format(format, months, monthDays);
                }
                int weeks = timeSpan.Days / 7;
                int weekDays = timeSpan.Days % 7;
                if (weekDays == 0)
                {
                    format = weeks == 1 ? resourceLoader.GetString("TimeSpan_Format_Week") : resourceLoader.GetString("TimeSpan_Format_Weeks");
                }
                else if (weekDays == 1)
                {
                    format = weeks == 1 ? resourceLoader.GetString("TimeSpan_Format_WeekAndDay") : resourceLoader.GetString("TimeSpan_Format_WeeksAndDay");

                }
                else // if (weekDays > 1)
                {
                    format = weeks == 1 ? resourceLoader.GetString("TimeSpan_Format_WeekAndDays") : resourceLoader.GetString("TimeSpan_Format_WeeksAndDays");
                }
                if (!string.IsNullOrEmpty(format))
                {
                    return string.Format(format, weeks, weekDays);
                }
            }

            if (timeSpan.Days > 0)
            {
                format = timeSpan.Days == 1 ? resourceLoader.GetString("TimeSpan_Format_Day") : resourceLoader.GetString("TimeSpan_Format_Days");
                if (string.IsNullOrEmpty(format))
                {
                    format = timeSpan.Days == 1 ? "1 day" : "{0} days";
                }
                return string.Format(format, timeSpan.Days);
            }

            if (timeSpan.Hours > 0)
            {
                format = timeSpan.Days == 1 ? resourceLoader.GetString("TimeSpan_Format_Hour") : resourceLoader.GetString("TimeSpan_Format_Hours");
                if (string.IsNullOrEmpty(format))
                {
                    format = timeSpan.Days == 1 ? "1 hour" : "{0} hours";
                }
                return string.Format(format, timeSpan.Hours);
            }

            if (timeSpan.Minutes > 0)
            {
                format = timeSpan.Minutes == 1 ? resourceLoader.GetString("TimeSpan_Format_Minute") : resourceLoader.GetString("TimeSpan_Format_Minutes");
                if (string.IsNullOrEmpty(format))
                {
                    format = timeSpan.Days == 1 ? "1 minute" : "{0} minutes";
                }
                return string.Format(format, timeSpan.Minutes);
            }

            format = timeSpan.Seconds == 1 ? resourceLoader.GetString("TimeSpan_Format_Second") : resourceLoader.GetString("TimeSpan_Format_Seconds");
            if (string.IsNullOrEmpty(format))
            {
                format = timeSpan.Seconds == 1 ? "1 second" : "{0} seconds";
            }
            return string.Format(format, timeSpan.Seconds);

        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
